package com.stone.persistent.recyclerview.widget;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;


public class GridItemDecoration extends RecyclerView.ItemDecoration {
    private  int divider;

    public void getItemOffsets( Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams)view.getLayoutParams();
            int spanIndex = layoutParams.getSpanIndex();
            if (spanIndex == 0) {
                outRect.left = this.divider;
                outRect.right = this.divider / 2;
            } else {
                outRect.right = this.divider;
                outRect.left = this.divider / 2;
            }

            outRect.bottom = this.divider;
            outRect.top = layoutParams.getViewAdapterPosition() < 2 ? this.divider : 0;
    }

    public GridItemDecoration(float divider) {
        this.divider = (int)divider;
    }
}
